# Maintainer: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
# Contribuitor: Alfredo Palhares <masterkorp@masterkorp.net>
# Contribuitor: Christian Babeux <christian.babeux@0x80.ca>

pkgbase=sysdig
pkgname=(sysdig sysdig-dkms)
pkgver=0.31.5
pkgrel=1

# from src/sysdig-*/cmake/modules/falcosecurity-libs.cmake
_falcover=0.10.5

pkgdesc="Open source system-level exploration and troubleshooting tool"
arch=('loong64' 'x86_64')
url="https://www.sysdig.com/"
license=('GPL2' 'Apache' 'MIT')
makedepends=('cmake' 'pandoc' 'gtest' 'jsoncpp' 'libjsoncpp.so' 'luajit' 'curl' 'libcurl.so' 'jq' 'libb64' 'intel-tbb' 'grpc' 'yaml-cpp' 're2')
source=("https://github.com/draios/sysdig/archive/$pkgver/$pkgbase-$pkgver.tar.gz"
        "falcosecurity-libs-$_falcover.tar.gz::https://github.com/falcosecurity/libs/archive/$_falcover.tar.gz"
        "bashcomp-location.patch"
        "falcosecurity-libs-nodownload.patch"
        "libsinsp-zstd.patch")
sha256sums=('9af98cae7c38273f7429ba0df628c9745bd92c949f444e180b9dd800af14c6dd'
            '2a4b37c08bec4ba81326314831f341385aff267062e8d4483437958689662936'
            '3b659326176c314eee9115adac39a249dc4b9530511b344ea6a2b23236bb8386'
            '3392204c265ef46c2a1378fc2acbb74b2b440585de4c9127a007f97ce10f0cfa'
            'bb29a1dd7a854019e2408b98fd954e73263a47f6da637556a83885175a729034')

prepare() {
  cd "$srcdir/libs-$_falcover"
  patch -p1 -i "$srcdir"/libsinsp-zstd.patch

  cd "$srcdir/$pkgbase-$pkgver"
  patch -p1 -i "$srcdir"/bashcomp-location.patch
  patch -p1 -i "$srcdir"/falcosecurity-libs-nodownload.patch

  sed s/USE_BUNDLED_DEPS/USE_BUNDLED_NJSON/ -i cmake/modules/nlohmann-json.cmake
# Fix build with abseil-cpp 2023
  sed -e 's|-std=c++0x||' -i CMakeLists.txt -i ../libs-${_falcover}/cmake/modules/CompilerFlags.cmake
}

build() {
  cd "$srcdir"/$pkgbase-$pkgver
  rm -rf build
  mkdir build
  cd build

  cmake .. \
        -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_CXX_FLAGS="${CXXFLAGS} ${CPPFLAGS} -Wno-deprecated-declarations" \
        -DCMAKE_C_FLAGS="${CFLAGS} ${CPPFLAGS} -Wno-deprecated-declarations" \
        -DCMAKE_CXX_STANDARD=17 \
        -DCMAKE_CXX_EXTENSIONS=OFF \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DSYSDIG_VERSION=$pkgver \
        -DDRIVER_VERSION=$_falcover \
        -DUSE_BUNDLED_DEPS=OFF \
        -DUSE_BUNDLED_TBB=OFF \
        -DUSE_BUNDLED_B64=OFF \
        -DUSE_BUNDLED_JSONCPP=OFF \
        -DUSE_BUNDLED_RE2=OFF \
        -DUSE_BUNDLED_VALIJSON=ON \
        -DUSE_BUNDLED_NJSON=ON \
        -DBUILD_DRIVER=OFF \
        -DBUILD_LIBSCAP_EXAMPLES=OFF
  make
}

package_sysdig() {
  optdepends=('sysdig-dkms: kernel module for live inspection')
  depends=('jsoncpp' 'libjsoncpp.so' 'luajit' 'curl' 'libcurl.so' 'jq' 'libb64' 'intel-tbb' 'grpc' 'libgrpc++.so' 'yaml-cpp' 'zstd')

  cd "$srcdir"/$pkgbase-$pkgver/build
  make install DESTDIR="$pkgdir"
  rm -rf "$pkgdir"/usr/src "$pkgdir"/sysdig

  install -dm755 "$pkgdir"/usr/share/licenses/$pkgname
  install -m644 "$srcdir"/$pkgbase-$pkgver/{NOTICES,COPYING} "$pkgdir"/usr/share/licenses/$pkgname
}

package_sysdig-dkms() {
  pkgdesc="DKMS kernel module for sysdig"
  depends=('sysdig' 'dkms')

  cd "$srcdir"/$pkgbase-$pkgver/build
  make install DESTDIR="$pkgdir"
  rm -rf "$pkgdir"/usr/{share,bin} "$pkgdir"/sysdig

  install -dm755 "$pkgdir"/usr/share/licenses/$pkgname
  install -m644 "$srcdir"/$pkgbase-$pkgver/{NOTICES,COPYING} "$pkgdir"/usr/share/licenses/$pkgname
}
